package com.qiniu.githubdata.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiniu.githubdata.entity.DeveloperTag;
import com.qiniu.githubdata.entity.Tag;
import com.qiniu.githubdata.entity.enumEntity.TagType;
import com.qiniu.githubdata.mapper.DeveloperTagMapper;
import com.qiniu.githubdata.mapper.TagMapper;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TagService extends ServiceImpl<TagMapper, Tag> {
    @Resource
    private DeveloperTagMapper developerTagMapper;
    public long getTagIdByTypeAndName(int type, String name) {
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        queryWrapper.eq("name", name);
        Tag tag = this.getOne(queryWrapper);
        if (tag == null) {
            return -1;
        }
        return tag.getId();
    }
    public List<String> getTagsByDeveloperId(long developerId) {
        List<DeveloperTag> dts = developerTagMapper.selectList(new QueryWrapper<DeveloperTag>().eq("developer_id", developerId));
        List<String> tags = dts.stream().map(dt -> {
            Tag tag = this.getById(dt.getTagId());
            return tag.getName();
        }).collect(Collectors.toList());
        return tags;
    }
    public List<Long> getDeveloperIdsByDeveloperTag( String language,String location) {
        // QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        if (language == null && location == null) {
            return new ArrayList<>();
        }

        List<Long> languageTagDeveloperIds = null;
        List<Long> locationTagDeveloperIds = null;

        // Get developers with language tag
        if (StringUtils.isNotEmpty(language)) {
            long languageTagId = getTagIdByTypeAndName(TagType.USER_LANGUAGE.getType(), language);
            if (languageTagId != -1) {
                QueryWrapper<DeveloperTag> languageWrapper = new QueryWrapper<>();
                languageWrapper.eq("tag_id", languageTagId);
                List<DeveloperTag> languageDeveloperTags = developerTagMapper.selectList(languageWrapper);
                languageTagDeveloperIds = languageDeveloperTags.stream()
                    .map(DeveloperTag::getDeveloperId)
                    .collect(Collectors.toList());
            }
        }

        // Get developers with location tag
        if (StringUtils.isNotEmpty(location)) {
            long locationTagId = getTagIdByTypeAndName(TagType.USER_LOCATION.getType(), location);
            if (locationTagId != -1) {
                QueryWrapper<DeveloperTag> locationWrapper = new QueryWrapper<>();
                locationWrapper.eq("tag_id", locationTagId);
                List<DeveloperTag> locationDeveloperTags = developerTagMapper.selectList(locationWrapper);
                locationTagDeveloperIds = locationDeveloperTags.stream()
                    .map(DeveloperTag::getDeveloperId)
                    .collect(Collectors.toList());
            }
        }

        // Return intersection of both lists if both tags exist
        if (languageTagDeveloperIds != null && locationTagDeveloperIds != null) {
            return languageTagDeveloperIds.stream()
                .filter(locationTagDeveloperIds::contains)
                .collect(Collectors.toList());
        } else if (languageTagDeveloperIds != null) {
            return languageTagDeveloperIds;
        } else {
            return locationTagDeveloperIds;
        }
        // return null;
    }
    public void addDeveloperLanguageTag(long developerId, String language) {
        if (StringUtils.isEmpty(language)) {
            return;
        }
        long tagId = getTagIdByTypeAndName(TagType.USER_LANGUAGE.getType(), language);
        if (tagId == -1) {
            Tag tag = new Tag();
            tag.setName(language);
            tag.setType(TagType.USER_LANGUAGE.getType());
            this.save(tag);
            //get the tag id
            QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type", TagType.USER_LANGUAGE.getType());
            queryWrapper.eq("name", language);
            tag = this.getOne(queryWrapper);
            tagId = tag.getId();
        }
        boolean exist = developerTagMapper.selectCount(new QueryWrapper<DeveloperTag>().eq("developer_id", developerId).eq("tag_id", tagId)) > 0;
        if(exist){
            return;
        }
        DeveloperTag developerTag = new DeveloperTag();
        developerTag.setDeveloperId(developerId);
        developerTag.setTagId(tagId);
        developerTagMapper.insert(developerTag);
    }

}
